package Lab02.Q8;
import java.text.NumberFormat;
public class getPi {
    public static void main(String[] args){
        int n=10;//位数
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(n);//不足两位不补0，超过两位部分的自动舍去
        System.out.println(nf.format(Get_pi(n)));

    }
    static double Get_pi(int n) {
        double p = 0;
        for (double i = 0; i < 5000000; i++) {
            double ptemp = p;
            p += 4*Math.pow(-1, i) / (2 * i + 1);
            if(Math.abs(p-ptemp)<=Math.pow(10,-n))
                break;
        }
        return p;
    }
}
